﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Brand
{
    public partial class Form : FormPage
    {
        BLL.Brand bll = new BLL.Brand();
        Model.Brand model = new Model.Brand();
        //
        BLL.Pic bllPic = new BLL.Pic();
        Model.Pic modelPic = new Model.Pic();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
            if (DataTypePlus.requestInt(model.PicID) > 0) modelPic = bllPic.GetModel(DataTypePlus.requestInt(model.PicID));
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Name.Text = model.Name;
            tb_Pic.Text = modelPic.URL;
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));

            if (
                Master.noErr(Name == "", "[品牌名称]必须填写") &&
                //PageValidate.noEmpty(Name, "[品牌名称]必须填写") &&
                Master.noChongfu("Name='" + Name + "'", "[品牌名称]重复")
                )
            {
                //图片
                string Pic = DataTypePlus.requestString(tb_Pic.Text);
                if (model.PicID > 0 && modelPic.URL != Pic) bllPic.Delete(modelPic.id);//删除老图
                if (Pic == "") model.PicID = 0;
                else if (modelPic.URL != Pic)
                {
                    modelPic.URL = Pic;
                    System.Drawing.Image bmp = System.Drawing.Image.FromFile(this.Server.MapPath(Server.UrlDecode(Pic)));
                    modelPic.Width = bmp.Width;
                    modelPic.Height = bmp.Height;
                    model.PicID = bllPic.Add(modelPic);
                }
                //
                model.Name = Name;
                if (Master.formState == EnumHelper.FormState.add) Master.id = bll.Add(model);
                else bll.Update(model);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Brand, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                Master.SubmitOK();

            }
        }
    }
}